package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;
import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.InterestReceiptData;
import com.wqtz.chechai.bean.RenewalReceiptData;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 :  Kisum
 * 创建时间: 2017/2/6
 */
public class RenewalApplicationActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.sb_back_money)
    SwitchButton mSbBackMoney;
    @BindView(R.id.rl_renewal_period)
    RelativeLayout mRlRenewalPeriod;
    @BindView(R.id.rl_upload_interest_receipt)
    RelativeLayout mRlUploadInterestReceipt;
    @BindView(R.id.rl_upload_renewal_receipt)
    RelativeLayout mRlUploadRenewalReceipt;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    @BindView(R.id.tv_interest_has_receipt)
    TextView mTvInterestHasReceipt;
    @BindView(R.id.tv_renewal_has_receipt)
    TextView mTvRenewalHasReceipt;
    private ArrayList<String> mRenewalReceiptDatas;
    private ArrayList<String> mInterestReceiptDatas;
    //续约审核中
    private final int RENEW_CHECKING = 13;

    @Override
    public void init() {
        ButterKnife.bind(this);
        //注册EventBus
        EventBus.getDefault().register(this);
        //设置SwitchButton监听
        mSbBackMoney.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LogUtils.e("isChecked:" + isChecked);
                mSbBackMoney.setBackColorRes(R.color.custom_back_color);
            }
        });
    }

    @Override
    public void loadData() {

    }

    @Override
    public int setContentViews() {
        return R.layout.activity_renewal_application;
    }

    @Override
    public BasePresent createPresenter() {
        return null;
    }

    @OnClick({R.id.iv_back, R.id.sb_back_money, R.id.rl_renewal_period, R.id.rl_upload_interest_receipt, R.id.rl_upload_renewal_receipt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            //续约抵押周期
            case R.id.rl_renewal_period:
                break;
            //上传利息回款回单
            case R.id.rl_upload_interest_receipt:
                Intent toUpLoadInterestReceiptPicIntent = new Intent(this, UpLoadInterestReceiptPicActivity.class);
                startActivity(toUpLoadInterestReceiptPicIntent);
                break;
            //上传续约声明
            case R.id.rl_upload_renewal_receipt:
                Intent toUpLoadRenewalReceiptPicIntent = new Intent(this, UpLoadRenewalReceiptPicActivity.class);
                startActivity(toUpLoadRenewalReceiptPicIntent);
                break;
        }
    }

    //订阅者订阅EventBus事件,在主线程执行，上传利息回款回单，更新UI
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void receiveInterestReceipt(InterestReceiptData interestReceiptData) {
        mInterestReceiptDatas = interestReceiptData.getList();
        LogUtils.e("interestReceiptDatas:" + mInterestReceiptDatas);
        if (mInterestReceiptDatas.size() == 0) {
            mTvInterestHasReceipt.setVisibility(View.INVISIBLE);
            mBtnConfirm.setClickable(false);
            mBtnConfirm.setBackgroundResource(R.drawable.shape_login_btn_default);
        } else {
            mTvInterestHasReceipt.setVisibility(View.VISIBLE);
            if (mRenewalReceiptDatas.size() != 0) {
                mBtnConfirm.setBackgroundResource(R.drawable.selector_activity_login_btn);
                btnConfirm();
            }
        }

    }


    //订阅者订阅EventBus事件,在主线程执行，上传续约声明，更新UI
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void receiveRenewalReceipt(RenewalReceiptData renewalReceiptData) {
        mRenewalReceiptDatas = renewalReceiptData.getList();
        LogUtils.e("imagePath:" + mRenewalReceiptDatas);
        if (mRenewalReceiptDatas.size() == 0) {
            mTvRenewalHasReceipt.setVisibility(View.INVISIBLE);
            mBtnConfirm.setClickable(false);
            mBtnConfirm.setBackgroundResource(R.drawable.shape_login_btn_default);
        } else {
            mTvRenewalHasReceipt.setVisibility(View.VISIBLE);
            if (mInterestReceiptDatas.size() != 0) {
                mBtnConfirm.setBackgroundResource(R.drawable.selector_activity_login_btn);
                btnConfirm();
            }
        }

    }
    //确认
    private void btnConfirm() {
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toLoanInfoIntent = new Intent(RenewalApplicationActivity.this, LoanInfoActivity.class);
                toLoanInfoIntent.putExtra("type", RENEW_CHECKING);
                startActivityForResult(toLoanInfoIntent,RENEW_CHECKING);
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        activitys.add(this); //添加当前的activity

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        EventBus.getDefault().removeAllStickyEvents();
    }

}
